package handler

import (
	"common/biz"
	"common/resp"
	"core/remote"
	"core/service"
	"encoding/json"
	"fmt"
	"game/logic"
	"game/models"
)

type GameHandler struct {
	um          *logic.UnionManager
	userService *service.UserService
}

// RoomMessageNotify 房间信息推送
func (h *GameHandler) RoomMessageNotify(session *remote.Session, msg []byte) any {
	if len(session.Msg.Uid) <= 0 {
		return resp.F(biz.InvalidUsers)
	}
	param := new(models.RoomMessageRequest)
	if err := json.Unmarshal(msg, param); err != nil {
		return resp.F(biz.RequestDataError)
	}
	roomId, exist := session.GetData("roomId")
	if !exist {
		return resp.F(biz.RoomNotExist)
	}
	room := h.um.GetRoomById(fmt.Sprintf("%v", roomId))
	if room == nil {
		return resp.F(biz.RoomNotExist)
	}
	room.RoomMessageHandler(session, param)
	return nil
}

// GameMessageNotify 看牌
func (h *GameHandler) GameMessageNotify(session *remote.Session, msg []byte) any {
	if len(session.Msg.Uid) <= 0 {
		return resp.F(biz.InvalidUsers)
	}
	roomId, exist := session.GetData("roomId")
	if !exist {
		return resp.F(biz.RoomNotExist)
	}
	room := h.um.GetRoomById(fmt.Sprintf("%v", roomId))
	room.GameMessageHandler(session, msg)
	return nil
}

func NewGameHandler(userService *service.UserService, um *logic.UnionManager) *GameHandler {
	return &GameHandler{um: um, userService: userService}
}
